package com.qidian.common.beanCache;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import com.qidian.common.beans.UserBean;

public final class UserBeanCache
{    
	private static UserBeanCache userBeanCache = new UserBeanCache() ;
	
	/* <用户名，用户>	*/
    private ConcurrentHashMap<String,UserBean> buffer  ;
    /* <ID，用户>*/
    private ConcurrentHashMap<Long,UserBean> bufId;
    
    private UserBeanCache()
    {
    	buffer = new ConcurrentHashMap<String,UserBean>(1024) ;
    	bufId = new ConcurrentHashMap<Long,UserBean>(1024) ;
    }
    
    public static UserBeanCache getInstance()
    {
    	return userBeanCache ;
    }
    
    public ConcurrentHashMap<String,UserBean> getBuffer()
    {
    	return buffer ;
    }
    
    public void add(UserBean userBean)
    {
    	if (userBean == null)
    		return ;
    	buffer.put(userBean.getUserName().toLowerCase(), userBean) ;
    	bufId.put(userBean.getUserId(), userBean) ;
    }
    public UserBean getByUserName(String userName)
    {
    	return buffer.get(userName.toLowerCase()) ;
    }
    
    public UserBean getById(Long id)
    {
    	return bufId.get(id) ;
    }
    
    public int size()
    {
    	return buffer.size() ;
    }
    
    public void remove(Long id)
    {
    	UserBean user = bufId.remove(id) ;
    	if (user != null)
    		buffer.remove(user.getUserName().toLowerCase()) ;
    }
    public void remove(String userName)
    {
    	UserBean user = buffer.remove(userName.toLowerCase()) ;
    	if (user != null)
    		bufId.remove(user.getUserId()) ;
    }
}
